/*
 *    Copyright © OpenAtom Foundation.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.inspur.edp.bff.core.session;

import com.inspur.edp.bef.core.session.FuncSession;
import com.inspur.edp.bff.api.manager.IFSManager;
import com.inspur.edp.bff.core.manager.BffBufferChangeManager;
import com.inspur.edp.bff.core.manager.FSManager;
import com.inspur.edp.bff.core.manager.IBffBufferChangeManager;
import com.inspur.edp.cef.api.response.viewrule.ViewRule;
import com.inspur.edp.cef.api.session.CefSessionItem;
import com.inspur.edp.commonmodel.core.session.distributed.DistributedSessionPart;
import com.inspur.edp.commonmodel.core.session.distributed.RootEditToken;
import com.inspur.edp.commonmodel.core.variable.VarBufferManager;
import lombok.Getter;
import lombok.Setter;

import java.util.HashMap;
import java.util.List;

public class BffSessionItem extends CefSessionItem implements DistributedSessionPart {

    public static final String Category = "BffSessionItem";

    private HashMap<String, Object> items;

    @Getter
    @Setter
    private VarBufferManager varBufferManager;
    private FuncSession session;
    private IFSManager manager;
    private String configId;
    private IBffBufferChangeManager bffBufferChangeManager;
    // 界面规则
    private ViewRule viewRule;
    // 界面规则中的隐藏字段在返回结果中的JSON路径，用于过滤掉返回结果中的隐藏字段
    private List<String> hiddenFieldsJsonPathInResult;
    // 界面规则中的不可更改字段在变更集中的JSON路径，用于检查变更集是否有恶意修改
    private List<String> unchangeableFieldsJsonPathInDataChange;

    public BffSessionItem(String configId) {
        setCategory(Category);
        setConfigId(configId);
        this.configId = configId;
    }

    public BffSessionItem(FuncSession session, String configId) {
        setCategory(Category);
        setConfigId(configId);
        this.configId = configId;
        this.session = session;
    }

    public HashMap<String, Object> getItems() {
        if (items == null) {
            items = new HashMap<>();
        }
        return items;
    }

    public void setItems(HashMap<String, Object> items) {
        this.items = items;
    }

    public FuncSession getSession() {
        return session;
    }

    public void setSession(FuncSession session) {
        this.session = session;
    }


    public IFSManager getManager() {
        return manager;
    }

    public void setManager(IFSManager manager) {
        this.manager = manager;
    }

    @Override
    public String getConfigId() {
        return configId;
    }

    @Override
    public void setConfigId(String configId) {
        this.configId = configId;
    }


    public IBffBufferChangeManager getBffBufferChangeManager() {
        if (bffBufferChangeManager == null) {
            bffBufferChangeManager = new BffBufferChangeManager(session, configId, ((FSManager) manager).getAccessorCreator());
        }
        return bffBufferChangeManager;
    }

    public void setBffBufferChangeManager(IBffBufferChangeManager bffBufferChangeManager) {
        this.bffBufferChangeManager = bffBufferChangeManager;
    }


    @Override
    public void endEdit(RootEditToken token) {
        if (varBufferManager != null) {
            varBufferManager.endEdit(token);
        }
    }

    @Override
    public void beginEdit(RootEditToken token) {
        if (varBufferManager != null) {
            varBufferManager.beginEdit(token);
        }
    }

    @Override
    public void notifySave() {
    }

    public ViewRule getViewRule() {
        return viewRule;
    }

    public void setViewRule(ViewRule viewRule) {
        this.viewRule = viewRule;
        // 以下两项依赖ViewRule，更改viewRule时需清理掉以避免使用时不匹配
        this.hiddenFieldsJsonPathInResult = null;
        this.unchangeableFieldsJsonPathInDataChange = null;
    }

    public List<String> getHiddenFieldsJsonPathInResult() {
        return hiddenFieldsJsonPathInResult;
    }

    public void setHiddenFieldsJsonPathInResult(List<String> hiddenFieldsJsonPathInResult) {
        this.hiddenFieldsJsonPathInResult = hiddenFieldsJsonPathInResult;
    }


    public List<String> getUnchangeableFieldsJsonPathInDataChange() {
        return unchangeableFieldsJsonPathInDataChange;
    }

    public void setUnchangeableFieldsJsonPathInDataChange(List<String> unchangeableFieldsJsonPathInDataChange) {
        this.unchangeableFieldsJsonPathInDataChange = unchangeableFieldsJsonPathInDataChange;
    }

}
